const fs = require('fs');
const http = require('http');
const server = http.createServer((request, response) => {
    response.setHeader('Content-Type', 'text/html;charset=utf-8');
    // 得到请求路径
    let {pathname} = new URL(request.url, 'http:localhost:8989');
    if (pathname === '/') {
        pathname = '/index.html'
    }
    // 得到当前文件的根目录
    const publicDir = __dirname + '/public';
    const filePath = publicDir + pathname;
    console.log(filePath);
    fs.readFile(filePath, (error, data) => {
        if(error) {
            response.end('<h4>  NOT FIND</h4>');
        }else {
            response.end(data);
        }
    })
})
server.listen('8989', () => {
    console.log('启动服务成功','http:localhost:8989');
})